import pandas as pd
import numpy as np


def write_excel(name):
    # 用pandas读取csv
    df = pd.read_csv(name + '.txt')[['data_type', 'date', 'region_name', 'x', 'y']]

    # 去除终端品牌列值小于0.1
    df = df.drop(df[(df['data_type'] == '终端品牌') & (df['y'].str.strip('%').astype(float) < 0.1)].index)

    # app日活
    write_by_type(df, 'App', name)

    # 年龄分布
    write_by_type(df, '年龄', name)

    # 性别分布
    write_by_type(df, '性别', name)

    # 终端分布
    write_by_type(df, '终端品牌', name)


def write_by_type(df, data_type, name):
    # 删除其它类型
    tdf = df.drop(df[(df['data_type'] != data_type)].index)

    # 取出区域
    regions = np.unique(tdf['region_name'].to_numpy()).tolist()

    # 取出分类
    types = np.unique(tdf['x'].to_numpy()).tolist()

    # 取出日期
    dates = np.unique(tdf['date'].to_numpy())

    # 构建需要输出的df
    new_csv_df = pd.DataFrame(columns=['维度', '日期', '分类'] + regions)
    for date in dates:
        for t in types:
            line = {'维度': data_type, '日期': date, '分类': t}

            for region in regions:
                val = "".join(tdf[(tdf['region_name'] == region) & (tdf['x'] == t) & (tdf['date'] == date)]['y'].values)
                line[region] = val

            new_csv_df = new_csv_df.append(pd.Series(line), ignore_index=True)

    # new_csv_df = new_csv_df.sort_values(['分类', '日期', '分类'])

    # 写入到csv
    file_name = name + "_" + data_type + '.csv'
    new_csv_df.to_csv(file_name)

    print(file_name, " 处理完成")


if __name__ == '__main__':
    # 1. 处理上海数据
    write_excel('上海市')

    # 2. 处理福建省数据
    write_excel('福建省')
